<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$return['params']=$params=array('uniacid'=>$_SESSION['uniacid']);
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','cid','aid','createtime','title','status');
	$list=pdo_getslice('jy_jdpm_news',$params,array($pindex,$psize),$total,$fields,'','createtime asc');
	if($list){
		foreach($list as &$item){	
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$account=pdo_getcolumn('jy_jdpm_role',array('id'=>$item['aid']),'name');
			$item['aid']=$account;
			$group=	pdo_get('jy_jdpm_goods_group',array('cid'=>$item['cid']),array('name'));
			$item['group']=$group['name'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'文章管理'),
			array('op'=>'edit','title'=>'添加文章'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'group','title'=>'分类','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'string'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'string'),
			array('name'=>'aid','title'=>'发布人','type'=>'string'),	
		),
		),
	);
	$return['status']=array('1'=>'已发布','0'=>'审核');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_jdpm_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	
	}else{
		$item=array();
	}
	$return['item']=$item;
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回文章列表'),
			
		),
		'desc'=>'文章编辑',
		'edit'=>array(
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'cid','title'=>'分类','type'=>'select','options'=>'groups','option_title'=>'name','option_value'=>'cid'),
			array('name'=>'status','title'=>'文章发布状态','type'=>'switch'),
			array('name'=>'is_hot','title'=>'是否头条','type'=>'switch'),
			array('name'=>'keywords','title'=>'文章关键字','type'=>'string','help'=>'多关键词间用半角逗号（,）分开，可用于做文章关联阅读条件'),
			array('name'=>'description','title'=>'文章描述','type'=>'text'),
			array('name'=>'summary','title'=>'文章摘要','type'=>'text'),
			array('name'=>'content','title'=>'文章内容','type'=>'richtext'),
		//	array('name'=>'picture','title'=>'文章图片','type'=>'image','help'=>'图片尺寸：宽180px；高100px'),	
			
		)
		);
	$return['groups']=pdo_getall('jy_jdpm_goods_group',array('uniacid'=>$_SESSION['uniacid']),array('cid','name'));
	$return['status']=array('1'=>'已发布','0'=>'审核');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	
	$data=array('is_hot'=>$post['is_hot'],'cid'=>$post['cid'],'title'=>trim($post['title']),'status'=>$post['status'],'keywords'=>trim($post['keywords']),'description'=>$post['description'],'summary'=>$post['summary'],'content'=>$post['content']);
	$data['aid']=1;
	if(!$id){
		$title=pdo_get('jy_jdpm_news',array('uniacid'=>$_SESSION['uniacid'],'title'=>trim($post['title'])));
	if($title){
		exi('已存在该标题文章，请修改标题');
	}
		$data['createtime']=time();
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_jdpm_news',$data);
		exi('添加成功','','list');
	}else{
		$data['update_time']=time();
		pdo_update('jy_jdpm_news',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功','','list');
	}
}
if($_RQ['op']=='groups'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid']
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	
	$list=pdo_getslice('jy_jdpm_goods_group',$params,array($pindex,$psize),$total,$fields);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'文章分类管理'),
			array('op'=>'groupsedit','title'=>'添加分类'),
		),
		'list'=>array('key'=>'cid','data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'cid','title'=>'CID','type'=>'string'),
			array('name'=>'name','title'=>'分类结构','type'=>'string'),
		
			array('name'=>'sort','title'=>'排序','type'=>'string'),
			

		),'action'=>array(
			array('op'=>'groupsedit','title'=>'编辑','type'=>'href'),
			array('op'=>'del2','title'=>'删除','show'=>array('pid >'=>0),'type'=>'post','message'=>'是否删除？'),		
			),
		),
		
		
	);
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='groupsedit'){
	$id=intval($_RQ['cid']);
	$item=pdo_get('jy_jdpm_goods_group',array('cid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['paixu']=$item['sort'];
	//print_r($item);exit();
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'文章分类',
		'tabs'=>array(
			array('op'=>'groups','title'=>'分类管理'),
			array('op'=>'groupsedit','title'=>'添加分类'),
		),
		'edit'=>array(
			array('type'=>'select','title'=>'选择父类','name'=>'pid','options'=>'groups','option_title'=>'name','option_value'=>'cid'),
			array('name'=>'name','title'=>'分类名称','type'=>'string'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
		)
	);
	$return['groups']=pdo_getall('jy_jdpm_goods_group',array('pid'=>0),array('cid','name'));
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($post['cid']);
	$data=array('pid'=>$post['pid'],'name'=>$post['name'],'sort'=>$post['paixu'],);
	if(!$id){
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_jdpm_goods_group',$data);
		exi('添加成功');
	}else{
		pdo_update('jy_jdpm_goods_group',$data,array('cid'=>$id));
		exi('修改成功');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jdpm_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='del2'){
	$id=intval($_RQ['cid']);
	if($id){
		pdo_delete('jy_jdpm_goods_group',array('cid'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_goods_group',array('cid'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}